#include "View.h"


extern float view_rotate[16];
extern float obj_pos[];

int Perspective::read(TiXmlElement *view)
{
	if(view->QueryFloatAttribute("near",&near)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("far",&far)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("angle",&angle)==TIXML_SUCCESS)
	{
		TiXmlElement *child=view->FirstChildElement();
		TiXmlElement *child2=child->NextSiblingElement();
		if(child->QueryFloatAttribute("x",&fromx)==TIXML_SUCCESS&&
		child->QueryFloatAttribute("y",&fromy)==TIXML_SUCCESS&&
		child->QueryFloatAttribute("z",&fromz)==TIXML_SUCCESS&&
		child2->QueryFloatAttribute("x",&tox)==TIXML_SUCCESS&&
		child2->QueryFloatAttribute("y",&toy)==TIXML_SUCCESS&&
		child2->QueryFloatAttribute("z",&toz)==TIXML_SUCCESS)
		{
		}
		else
		{
			//cout<<"ERRO NA LEITURA DE VISTA PERSPECTIVA\n";
			return -1;
		}

	}
	else
	{
		//cout<<"ERRO NA LEITURA DE VISTA PERSPECTIVA\n";
		return -1;
	}
	return 1;
}
int Perspective::make()
{
	// inicializacoes da matriz de visualizacao
	glMatrixMode( GL_PROJECTION );
	if(Nodo::mode!=GL_SELECT)glLoadIdentity();
	int tx,ty,tw,th;
	GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
	float xy_aspect = (float)tw / (float)th;
	
	glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, near, far);
	//inicializacoes da matriz de transformacoes geometricas
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
	gluLookAt(fromx,fromy,fromz,tox,toy,toz,0,1,0);
	glMultMatrixf( view_rotate );
	return 1;
	////cout<<"PERSPECTIVE\n";
}

int Ortho::read(TiXmlElement *view)
{
	if(view->QueryFloatAttribute("near",&near)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("far",&far)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("left",&left)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("right",&right)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("top",&top)==TIXML_SUCCESS&&
	view->QueryFloatAttribute("bottom",&bottom)==TIXML_SUCCESS)
	{}
	else
	{
		//cout<<"ERRO NA LEITURA DE VISTA ORTOGONAL\n";
		return -1;
	}
	return 1;
}
int Ortho::make()
{
	// inicializacoes da matriz de visualizacao
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho(left,right,bottom,top,near,far);
	
	
	//inicializacoes da matriz de transformacoes geometricas
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	
	////cout<<"ORTHO\n";
	return -1;
}